package org.ccay.excel.meta;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ccay.core.util.StringUtil;

/**
 * Excel数据列的元数据信息。
 * 	
 * @author chaowangbang
 * @since Mar 16, 2012
 */
public class ColumnMeta extends NamedElement implements java.lang.Cloneable{
	
	/**
	 * 列的数据类型
	 */
	private String type;
	
	/**
	 * 列对应的VO属性名称
	 */
	private String fieldName;
	
	/**
	 * 给列的title增加注释
	 */
	private String annotation;
	
	/**
	 * Column的列消费者对应的Spring Bean
	 */
	private String extentionBean;
	
	/**
	 * 动态可配项
	 */
	private String dynamicItem;
	
	public String getDynamicItem() {
		return dynamicItem;
	}

	public void setDynamicItem(String dynamicItem) {
		this.dynamicItem = dynamicItem;
	}

	public String getExtentionBean() {
		return extentionBean;
	}

	public void setExtentionBean(String extentionBean) {
		this.extentionBean = extentionBean;
	}
	
	/**
	 * @return the annotation
	 */
	public String getAnnotation() {
		return annotation;
	}

	/**
	 * @param annotation the annotation to set
	 */
	public void setAnnotation(String annotation) {
		this.annotation = annotation;
	}

	/**
	 * 验证器列表
	 */
	private List<ValidatorMeta> validators = new ArrayList<ValidatorMeta>();
	
	/**
	 * @param name
	 */
	public ColumnMeta(String name) {
		super(name);
	}

	/** 获取列对应的VO属性名称
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getFieldName() {
		return fieldName;
	}

	/** 设置列对应的VO属性名称	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param fieldName
	 */
	public void setFieldName(String fieldName) {
		this.fieldName = fieldName;
	}

	/** 获取列的数据类型	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public String getType() {
		return type;
	}

	/** 设置列的数据类型	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param type
	 */
	public void setType(String type) {
		this.type = type;
	}
	
	/** 添加一个验证器定义	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @param validator
	 */
	public void addValidator(ValidatorMeta validator) {
		this.validators.add(validator);
	}
	
	/** 获取验证器列表	
	 * @author chaowangbang
	 * @since Mar 19, 2012
	 * @return
	 */
	public List<ValidatorMeta> getValidators() {
		return this.validators;
	}
	
	/**获取指定的验证器配置
	 * @param type
	 * @return
	 */
	public ValidatorMeta getValidatorMeta(String type) {
		if(StringUtil.isNullOrEmpty(type)) {
			return null;
		}
		
		for(int i=0; i<this.validators.size(); i++) {
			ValidatorMeta v = this.validators.get(i);
			if(type.equals(v.getType())) {
				return v;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.meta.NamedElement#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		return super.equals(obj);
	}

	/* (non-Javadoc)
	 * @see org.ccay.excel.meta.NamedElement#hashCode()
	 */
	@Override
	public int hashCode() {
		return super.hashCode();
	}
	
	public ColumnMeta copy() throws CloneNotSupportedException{
		ColumnMeta copyColumnMeta = (ColumnMeta) this.clone();
		List<ValidatorMeta> copyValidators = new ArrayList<ValidatorMeta>();
		for(ValidatorMeta meta:this.getValidators()){
			copyValidators.add(meta.copy());
		}
		copyColumnMeta.validators = copyValidators;
		HashMap<String,String> copyLanguageNames = (HashMap<String,String>) this.getLanguageNames();
		copyColumnMeta.setLanguageNames((HashMap<String,String>)copyLanguageNames.clone());
		return copyColumnMeta;
	}
}
